﻿using System.Configuration;
using System.Data;
using System.Windows;
using SoonCode.FocusTest.Views;
using Prism.DryIoc;
using Prism.Ioc;
using SoonCode.Core;
using SoonCode.FocusTest.ViewModels;
using SoonCode.FocusTest.Views.Teacher;
using MessageBox = HandyControl.Controls.MessageBox;


namespace SoonCode.FocusTest
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        public App()
        {
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = false;
        }

        //线程异常探知
        private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
#if !DEBUG

            FileHelper.WriteLog(e.ToString(), DateTime.Now.ToString("yyyy-MM-dd")+".log");
            e.SetObserved();
#endif
        }
        //
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
#if !DEBUG
            Exception ex = (Exception)e.ExceptionObject;
            FileHelper.WriteLog(ex.Message, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
            FileHelper.WriteLog(ex.StackTrace, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
            MessageBox.Show("系统发生异常，如果重复出现这个问题请联系系统提供商，并提供日志，以解决该问题！",
                "系统异常", MessageBoxButton.OK, MessageBoxImage.Error);
#endif
        }

        //全局异常处理
        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
#if !DEBUG
            e.Handled = true;
            FileHelper.WriteLog(e.Exception.Message, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
            FileHelper.WriteLog(e.Exception.StackTrace, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
            MessageBox.Show("系统发生异常，如果重复出现这个问题请联系系统提供商，并提供日志，以解决该问题！",
                "系统异常", MessageBoxButton.OK, MessageBoxImage.Error);
#endif
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterDialogWindow<DialogWindowView>("myDialogWindow");
            containerRegistry.RegisterDialog<MemberList,MemberListViewModel>();
            containerRegistry.RegisterDialog<SelectTeacher,SelectTeacherViewModel>();
            containerRegistry.RegisterDialog<InputBox,InputBoxViewModel>();
            containerRegistry.RegisterDialog<SettingView,SettingViewModel>();
            containerRegistry.RegisterDialog<MemberEditView,MemberEditViewModel>();
            containerRegistry.RegisterDialog<MemberFeeListView,MemberFeeListViewModel>();
            containerRegistry.RegisterDialog<MemberFeeEditView,MemberFeeEditViewModel>();
            containerRegistry.RegisterSingleton<TestPrintView>();
            containerRegistry.RegisterSingleton<SensoryPrintView>();
        }

        protected override Window CreateShell()
        {
            var w = Container.Resolve<Main>();
            Container.Resolve<TestPrintView>();
            Container.Resolve<SensoryPrintView>();
            return w;
        }
    }

}
